#include "stdafx.h"
#include "DropTarget.h"
#include "ImportProgressDialog.h"

HRESULT STDMETHODCALLTYPE CDropTarget::DragEnter( 
        /* [unique][in] */ __RPC__in_opt IDataObject *pDataObj,
        /* [in] */ DWORD grfKeyState,
        /* [in] */ POINTL pt,
        /* [out][in] */ __RPC__inout DWORD *pdwEffect)
{
	/* This is called before Drop() */
	UNREFERENCED_PARAMETER(pDataObj);
	UNREFERENCED_PARAMETER(grfKeyState);
	UNREFERENCED_PARAMETER(pt);

	*pdwEffect = DROPEFFECT_COPY;
	return S_OK;
}
    
HRESULT STDMETHODCALLTYPE CDropTarget::DragOver( 
        /* [in] */ DWORD grfKeyState,
        /* [in] */ POINTL pt,
        /* [out][in] */ __RPC__inout DWORD *pdwEffect)
{
	/* This doesn't appear to be called; but we'll do something appropriate anyway */
	UNREFERENCED_PARAMETER(grfKeyState);
	UNREFERENCED_PARAMETER(pt);

	*pdwEffect = DROPEFFECT_COPY;
	return S_OK;
}
    
HRESULT STDMETHODCALLTYPE CDropTarget::DragLeave()
{
	/* This doesn't appear to be called; but we'll do something appropriate anyway */
	return S_OK;
}

HRESULT STDMETHODCALLTYPE CDropTarget::Drop( 
        /* [unique][in] */ __RPC__in_opt IDataObject *pDataObj,
        /* [in] */ DWORD grfKeyState,
        /* [in] */ POINTL pt,
        /* [out][in] */ __RPC__inout DWORD *pdwEffect)
{
	UNREFERENCED_PARAMETER(grfKeyState);
	UNREFERENCED_PARAMETER(pt);

	FORMATETC etc;
	memset(&etc, 0, sizeof(FORMATETC));
	etc.cfFormat = CF_HDROP;
	etc.lindex = -1;
	etc.dwAspect = DVASPECT_CONTENT;
	etc.tymed = TYMED_HGLOBAL;

	STGMEDIUM stg;
	memset(&stg, 0, sizeof(STGMEDIUM));

	HRESULT hr;
	if (SUCCEEDED(hr = pDataObj->GetData(&etc, &stg)))
	{
		HDROP hDrop = (HDROP)stg.hGlobal;

		CImportProgressDialog dlg;

		UINT fileCount = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
		for (UINT i = 0; i < fileCount; ++i)
		{
			UINT fileNameLength = DragQueryFile(hDrop, i, NULL, 0);

			CString strPath;
			DragQueryFile(hDrop, i, strPath.GetBuffer(fileNameLength + 1), fileNameLength + 1);
			strPath.ReleaseBuffer();

			dlg.AddDroppedFile(strPath);
		}

		ReleaseStgMedium(&stg);
		dlg.DoModal();
	}

	*pdwEffect = DROPEFFECT_COPY;
	return S_OK;
}
